<?php

class UserModel extends Model {

    /**
     * 获取用户ID
     * @return type 
     */
    public static function auth($sid = "")
	{
		$sid = $sid ? $sid : trim($_POST["sid"]);
		$sid = $sid ? $sid : trim($_GET["sid"]);
		//$sid = $sid ? $sid : trim($_COOKIE["sid"]);
		$sid = self::hexToStr(strtoupper($sid));
        $login = xtea_decrypt(base64_decode($sid), AUTHKEY);
        $login = $login ? explode("|", $login) : array();
        if ($login && $login[0])  return $login[0];
    }

    /**
     * 用户退出
     */
    public function quit()
	{
        //set_cookie("sid", "");
        set_cookie("ssid", "");
        set_cookie("skey", "");

		//set_cookie("sid", "", 0, "/", "." . ROOTDOMAIN);
		set_cookie("ssid", "", 0, "/", "." . ROOTDOMAIN);
		set_cookie("skey", "", 0, "/", "." . ROOTDOMAIN);
		set_cookie("sspara", "", 0, "/", "." . ROOTDOMAIN);
    }

    /**
     * 用户登陆
     * @return type 
     */
    public static function login()
	{
        load_sns();
        $sns = new kra(SNS_API_NAME, SNS_API_KEY, SNS_API_SECRET, array("app_id" => SNS_API_ADT_ID, "app_name" => SNS_API_ADT_NAME, "app_key" => SNS_API_ADT_KEY));
        $oid = $sns->get_uid();
        if (!$oid)
		{
            $sns->request_permissions(SNS_AUTH_CALLBACK_URL);
            return false;
        }
        $user = $sns->me(array("nickname", "thumbnail_url", "gender", "age"));
        if (!$user || !$user['nickname'])
		{
            $sns->request_permissions(SNS_AUTH_CALLBACK_URL);
            return false;
        }
        $user["gender"] = $user["gender"] == "MALE" ? 1 : ($user["gender"] == "FEMALE" ? 2 : 0);

		if(!$sns->isfan()) $sns->becomefan();

		$uidinfo = daocall("user", "oid2uidinfo", array($oid));
		$uid = intval($uidinfo['uid']);
        if ($uid)
		{
			$tm = time();
			$logintm = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
			$ldays = floor(($tm - intval($uidinfo['rtm'])) / 86400 + 0.5);

			if($ldays >= 0 && $ldays <= 30)
			{
				if($ldays <= 7)
				{
					$olday = "l{$ldays}";
					if(!$uidinfo[$olday]) $logintm[$ldays] = 1;
				}
				elseif($ldays > 7 && $ldays <= 15)
				{
					if(!$uidinfo['l8']) $logintm[8] = 1;
				}
				else
				{
					if(!$uidinfo['l9']) $logintm[9] = 1;
				}
			}

			$logintm['lastlogin'] = 1;
			daocall("user", "setrlogin", array($uid, $logintm));

            daocall("user", "setuser", array($uid, $user["nickname"], $user["thumbnail_url"], $user["gender"], $user["age"]));
			
			$sid = self::strToHex((base64_encode(xtea_encrypt($uid, AUTHKEY))));

            $uinfo = daocall("user", "getuserext", array($uid));
			if (!$uinfo)
			{
				echo "<script>window.top.location.href='" . SNS_AUTH_CALLBACK_URL . "';</script>";
				exit();
			}

            $lv = self::xp2lv($uinfo['xp'] ? $uinfo['xp'] : 0);
			daocall("cysstatic", "login", array($uid, $lv));

            return array(
				"uid" => $uid, 
				"oid" => $oid, 
				"name" => $user["nickname"], 
				"thumb" => $user["thumbnail_url"], 
				"xp" => $uinfo['xp'],
				"xp" => $uinfo['xp'],
				"xp" => $uinfo['xp'],
				//"lv" => $lv,
				"sid"	=> $sid,
			);
        }
        return self::reg($oid, $user["nickname"], $user["thumbnail_url"], $user["gender"], $user["age"]);
    }
	
    /**
     * 用户注册
     * @param type $oid
     * @param type $name
     * @param type $thumb
     * @param type $sex
     * @param type $age
     * @return type 
     */
    public static function reg($oid = "", $name = "", $thumb = "", $sex = 0, $age = 0)
	{/* {{{ */
        $uid = daocall("user", "newuid", array($oid));
        if (!$uid)
		{
			self::setcmslog(2, array("oid" => $oid), "reg->newuid faild.");
            return false;
        }
        if (!daocall("user", "newuser", array($uid, $oid, $name, $thumb, $sex, $age)))
		{
            self::setcmslog(2, array("oid" => $oid, "uid" => $uid), "reg->newuser faild.");
            return false;
        }

		if(!daocall("user", "newuserext", array($uid, array('gb' => 500, 'mb' => 12, 'ps' => 5))))
		{
			self::setcmslog(2, array("oid" => $oid), "reg->setinit faild.");
            return false;
		}

		if(!daocall("user", "setinit", array($uid)))
		{
			self::setcmslog(2, array("oid" => $oid), "reg->setinit faild.");
            return false;
		}

        $sid = self::strToHex(base64_encode(xtea_encrypt($uid, AUTHKEY)));

		daocall("cysstatic", "newuser", array($uid));
        return array("uid" => $uid, "oid" => $oid, "name" => $name, "thumb" => $thumb, "lv" => 1, "sid" => $sid);
    }
        
	public static function info($uid = 0)
	{
        $uid = intval($uid);
        if (!$uid) return array('ecode' => ERR302);
        
        $user = daocall("user", "getuser", array($uid));
        if (!$user) return array('ecode' => ERR401);

        $ext = daocall("user", "getuserext", array($uid));
        if ($ext === false || !is_array($ext)) return array('ecode' => ERR402);
        
		return array('ecode' => 0, 'ret' => array_merge($user, $ext));
    }

	/**
     * 获取用户信息
     * @param type $uid
     * @return type 
     */
    public static function baseinfo($uid = 0)
	{
        $uid = intval($uid);
        if (!$uid) return array('ecode' => ERR302);
        
        $user = daocall("user", "getuser", array($uid));
        if (!$user) return array('ecode' => ERR401);

        $user['uid'] = $uid;
        return array('ecode' => 0, 'ret' => $user);
    }

    /**
     * 获取用户游戏数据信息
     * @param type $uid
     * @return type 
     */
    public static function exinfo($uid = 0)
	{
        $uid = intval($uid);
        if (!$uid) return array('ecode' => ERR302);

        $ext = daocall("user", "getuserext", array($uid));
        if (!$ext || !is_array($ext)) return array('ecode' => ERR402);

        return array('ecode' => 0, 'ret' => $ext);
    }

    /**
     * 经验转换为等级
     * @param type
     * @return type 
     */
    public static function xp2lv($xp = 0)
	{
        $conf = &load_conf("pub:user");
        $lvconf = &$conf['lvxp'];
        $lv = 0;
        $s = 0;
		foreach($lvconf as $nlv => $nxp)
		{
			if($xp < $nxp) break;
			$lv = $nlv;
		}
        return $lv;
    }

	//设置阵营
	public static function setside($uid = 0, $side = '')
	{
		$side = $side == 'a' ? 'a' : 'b';

		if(!daocall("user", "setuserext", array($uid, array("side" => $side)))) return array('ecode' => ERR407);

		return array("ecode" => SUCCESS, 'ret' => $side);
	}
}
?>